<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_penandatangan extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'id_ttd';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$jabatan_ttd = isset($_POST['jabatan_ttd']) ? strval($_POST['jabatan_ttd']) : '';
		$nip_ttd = isset($_POST['nip_ttd']) ? strval($_POST['nip_ttd']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*');
			$this->db->from('ttd');
			$this->db->where('status','A');
			#Kimochi - otoritas
				#--Faklutas
			/* if($this->session->userdata('kd_fakultas')!=''){
				$this->db->where('b.kd_fakultas', $this->session->userdata('kd_fakultas'));
			}else{
				if($kd_fakultas != '')
					$this->db->where('b.kd_fakultas', $kd_fakultas);
			}
				#--Prodi
			if($this->session->userdata('kd_prodi')!=''){
				$this->db->where('a.kd_prodi', $this->session->userdata('kd_prodi'));
			}else{
				if($kd_prodi != '')
					$this->db->where('a.kd_prodi', $kd_prodi);
			}
			#End Kimochi - otoritas
			*/

			if($jabatan_ttd != ''){
				$this->db->like('jabatan_ttd', $jabatan_ttd, 'both');
			}
			
			if($nip_ttd != ''){
				$this->db->like('nip_ttd', $nip_ttd, 'both');
			}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getDataEdit($id_ttd){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('ttd');
		$this->db->where('id_ttd', $id_ttd);
		return $this->db->get();
	}
	
	function getdataByNIM($nim){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('tbl_mahasiswa a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = b.kd_fakultas');
		$this->db->join('ref_program d', 'd.kd_program = a.kd_program');
		$this->db->where('a.nim', $nim);
		
		return $this->db->get();
	}
	
	function togrid($data, $count=0){
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
			$response->total = $count;
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function getDataCount($status=''){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('ttd');
		
		return $this->db->get()->num_rows();
	}
	
	function getDataChart($data){
		$i=0;
		foreach($data as $d){
			$this->db->flush_cache();
			$this->db->select('*');
			$this->db->from('warga');
			$this->db->where('thn_daftar', $d);
			
			$tmp[$i] = $this->db->get()->num_rows();
			$i++;
		}
		return $tmp;
	}
	
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('id_ttd',$kode);
		
		$this->db->select('*');
		$this->db->from('ttd');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('id_ttd', $data['id_ttd']);
		$this->db->set('jabatan_ttd', $data['jabatan_ttd']);
		$this->db->set('nama_ttd', $data['nama_ttd']);
		$this->db->set('nip_ttd', $data['nip_ttd']);
		$this->db->set('status', 'A');
		$result = $this->db->insert('ttd');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}

	function DeleteOnDb($id_ttd){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "D");
	   
		//$this->db->set('MUID', $this->session->userdata('user_id'));
		//$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_ttd', $id_ttd);
		$result = $this->db->update('ttd');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}

	function UpdateOnDb($data){
		$this->db->flush_cache();
		$this->db->set('id_ttd', $data['id_ttd']);
		$this->db->set('jabatan_ttd', $data['jabatan_ttd']);
		$this->db->set('nama_ttd', $data['nama_ttd']);
		$this->db->set('nip_ttd', $data['nip_ttd']);
		$this->db->set('status', "A");
		$this->db->where('id_ttd', $data['id_ttd']);

		$result = $this->db->update('ttd');

		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
}

?>